document.addEventListener('DOMContentLoaded', function() {
    // 移动设备菜单切换（如果需要）
    const mobileMenuButton = document.createElement('button');
    mobileMenuButton.className = 'mobile-menu-button';
    mobileMenuButton.innerHTML = '<i class="fas fa-bars"></i> 菜单';
    
    const mainNav = document.querySelector('.main-nav');
    if (mainNav) {
        mainNav.parentNode.insertBefore(mobileMenuButton, mainNav);
        
        mobileMenuButton.addEventListener('click', function() {
            mainNav.style.display = mainNav.style.display === 'none' ? 'block' : 'none';
        });
        
        // 初始状态根据屏幕宽度决定
        function checkMenu() {
            if (window.innerWidth <= 768) {
                mainNav.style.display = 'none';
                mobileMenuButton.style.display = 'block';
            } else {
                mainNav.style.display = 'block';
                mobileMenuButton.style.display = 'none';
            }
        }
        
        checkMenu();
        window.addEventListener('resize', checkMenu);
    }
    
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            
            const targetId = this.getAttribute('href');
            if (targetId === '#') return;
            
            const targetElement = document.querySelector(targetId);
            if (targetElement) {
                window.scrollTo({
                    top: targetElement.offsetTop - 100,
                    behavior: 'smooth'
                });
            }
        });
    });
    
    // 回到顶部按钮
    const backToTop = document.createElement('div');
    backToTop.className = 'back-to-top';
    backToTop.innerHTML = '<i class="fas fa-arrow-up"></i>';
    document.body.appendChild(backToTop);
    
    backToTop.addEventListener('click', function() {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });
    
    window.addEventListener('scroll', function() {
        if (window.pageYOffset > 300) {
            backToTop.style.display = 'block';
        } else {
            backToTop.style.display = 'none';
        }
    });
    
    backToTop.style.display = 'none';
});

// 添加一些样式到back-to-top按钮
const backToTopStyle = document.createElement('style');
backToTopStyle.textContent = `
    .back-to-top {
        position: fixed;
        bottom: 30px;
        right: 30px;
        width: 50px;
        height: 50px;
        background: #2a6496;
        color: white;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        opacity: 0.8;
        transition: opacity 0.3s;
        z-index: 999;
    }
    
    .back-to-top:hover {
        opacity: 1;
    }
    
    .mobile-menu-button {
        display: none;
        width: 100%;
        padding: 1rem;
        background: #1a4466;
        color: white;
        border: none;
        text-align: center;
        cursor: pointer;
    }
    
    @media (max-width: 768px) {
        .mobile-menu-button {
            display: block;
        }
    }
`;
document.head.appendChild(backToTopStyle);